// 获取所有试题信息
let testInfo = JSON.parse(localStorage.getItem("testInfo"));
console.log(testInfo);

//渲染
render();
function render() {
  $(".username").text(userInfo.name);
  $(".img").attr("src", userInfo.avatar);
}

// 定时器
let timer;
setTime();
function setTime() {
  let time = parseInt(testInfo.time);
  time = time * 60 * 1000;
  timer = setInterval(() => {
    time -= 1000;
    let h = Math.floor((time / 1000 / 60 / 60) % 24);
    let m = Math.floor((time / 1000 / 60) % 60);
    let s = Math.floor((time / 1000) % 60);
    $(".hour").text(h + "0");
    $(".minute").text(m);
    $(".second").text(s);
  }, 1000);
}

let currentPage = 0;

// 上一题
$("#preExe").click(function () {
  if (currentPage <= 0) return alert("前面没有了");
  currentPage--;
  $(`.exe${currentPage}`).show().siblings().hide();
  $("#currentPage").text(currentPage + 1);

  // console.log(currentPage);
});

// 下一题
$("#nextExe").click(function () {
  if (currentPage >= testInfo.exercisesId.length - 1)
    return alert("已经到底了");
  currentPage++;
  $(`.exe${currentPage}`).show().siblings().hide();
  $("#currentPage").text(currentPage + 1);
  // console.log(currentPage);
});

//渲染试题
renderExer();
function renderExer() {
  let exe = testInfo.exercisesId;
  // console.log(exe);
  exe.forEach((v, index) => {
    $("#exe").append(`
    <div class='exe${index}' style='display:none'>
     <div class="questions">
              <span>${index + 1}.</span>
              <p class="question">${v.topics}</p>
            </div>
            <div class="slectAnswer" data-index='${index}'>
              <div class="item  " >
                <span class="slect">A</span>
                <span>${v.options[0]}</span>
              </div>
              <div class="item " >
                <span class="slect">B</span>
                <span>${v.options[1]}</span>
              </div>
              <div class="item " >
                <span class="slect">C</span>
                <span>${v.options[2]}</span>
              </div>
              <div class="item " >
                <span class="slect">D</span>
                <span>${v.options[3]}</span>
              </div>
            </div>
    </div>
  `);
  });

  $(`.exe0`).show();
  $(".total").text(testInfo.exercisesId.length);
}

// 错题数组
let errorArr = [];

// 选题添加样式
$(".item").on("click", function () {
  $(this).toggleClass("active");
  $(this).siblings().removeClass("active");
});

//点击交卷按钮弹框
$(".click-up").click(function () {
  $(".total").text(testInfo.exercisesId.length);
  $(".exedNum").text($(".active").length);
  $(".notExe").text(testInfo.exercisesId.length - $(".active").length);
});

//确认交卷
$("#confirmBtn").click(function () {
  clearInterval(timer);
  localStorage.score = computedScore();
  // 已考试卷提交
  postTested();
  location.href = `./start-test-3.html`;
});
// 分数
let score = 0;
//统计分数
function computedScore() {
  // 题目数组
  let tests = testInfo.exercisesId;
  // console.log(tests);
  $(".slectAnswer").each((i, v) => {
    // 当前选择题目的下标
    let optionIndex = $(v).find(".active").index();
    tests[i].studentOption = optionIndex;

    if (optionIndex == tests[i].answer) score += 3;
    if (optionIndex != tests[i].answer) errorCollect(i, optionIndex);
  });
  //再保存本地
  // console.log(tests);
  localStorage.detailPageData = JSON.stringify(tests);

  return score;
}

// 错题收集
function errorCollect(ei, oi) {
  let studentId = userInfo._id;
  let exerciseId = testInfo.exercisesId[ei]._id;
  let errorAnswer = oi > 0 ? oi : 4;

  let errObj = {
    studentId,
    exerciseId,
    errorAnswer
  };
  postErrArr(errObj);
}

//发送错题到服务器 点击交卷触发
function postErrArr(errObj) {
  console.log(errObj);
  $.ajax({
    url: "/errors/createErrors",
    type: "POST",
    data: errObj,
    success(res) {
      // console.log(res);
    }
  });
}

// 添加试卷
function postTested() {
  // console.log(score);
  // console.log(userInfo._id);
  // console.log(testInfo._id);
  $.ajax({
    url: "/tested/saveTest",
    type: "POST",
    data: { score, studentId: userInfo._id, testId: testInfo._id },
    success(res) {
      console.log(res);
    }
  });
}
